//var request = require('request');
var FeedParser = require('feedparser');
var rssSite = require('../config/rssSite.json');
var rssUtil = require('../util/rss.js');
var htmlUtil = require('../util/html.js');

exports.getBlogs = getBlogs;

function getBlogs(callback) {
    var channels = rssSite.channel;

    channels.forEach(function(channel, err) {
        if (channel.work != false) {
            //console.log("begin:" + channel.title);
            rssUtil.fetch(channel.link, docHandler, function(docs) {
                callback(docs, err);
            });
        }
    });
};

// convert rss doc to article model
function docHandler(doc) {
    var index = doc.guid.indexOf("post");
    var c = htmlUtil.getContent('<body>' + doc.description + '</body>', 'body');
    var article = {
        id: doc.guid.substr(index),
        title: doc.title,
        content: c.context.substr(0, 200) + "......",
        publish: Date.parse(doc.pubdate),
        link: doc.link.replace('binaryware.blogspot','blog.binwh'),
        imageLink: c.imgPath,
    };
    return article;
};